<?php

namespace app\util;

use app\util\Utils;
use think\Cache;
use think\Db;

/*
 * 直播
 */
class Webcast
{
    //登录名
    private $loginName;

    //密码
    private $password;

    //接口公共url
    private $url = 'http://kekemei.gensee.com/integration/site/';

    //默认直播信息
    private $defaultWebcastInfo = [
        'img' => 'static/live/livee.png',
        'avatar' => 'static/live/logo.png',
        'nickname' => '克克美',
    ];

    /*
     * 构造方法
     */
    public function __construct()
    {
        $this->loginName = config('appconfig.webcast_login_name');
        $this->password = config('appconfig.webcast_password');
    }

    /*
     * 获取直播自定义信息
     * 从数据库获取对应直播的封面，主播头像，主播名称
     */
    public function getWebcastMyInfo($webcastId)
    {
        $key = 'webcast_' . $webcastId;

        //$row = Cache::get($key);
        $row = null;
        if (!is_null($row)) {
            return $row;
        } else {
            $row = Db::name('livecover')
                ->where('number', '=', $webcastId)
                ->field('img, avatar, nickname')
                ->find();

            if (is_null($row)) {
                return $this->defaultWebcastInfo;
            } else {
                Cache::set($key, $row);

                return $row;
            }
        }
    }

    /*
     * 获取点播播放次数
     * $number 点播编号
     */
    public function getWebcastVodNumber($number)
    {
        $key = 'webcast_vod_' . $number;

        $vodCount = Cache::get($key);
        if ($vodCount) {
            return $vodCount;
        } else {
            $row = Db::name('vodnumber')
                ->where('number', '=', $number)
                ->field('vod_count')
                ->find();

            if (is_null($row)) {
                return 0;
            } else {
                Cache::set($key, $row['vod_count']);

                return $row['vod_count'];
            }
        }
    }

    /*
     * 获取直播提醒状态
     */
    public function getWebcastNotifyStatus($staff_id, $webcastId)
    {
        $notify_status = 0;

        $row = Db::name('remindme')->where('staff_id', '=', $staff_id)
            ->where('live_id', '=', $webcastId)
            ->find();
        if (!is_null($row)) {
            $notify_status = 1;
        }

        return $notify_status;
    }

    /*
     * 分页获取站点直播列表
     * 对应接口 5.9 webcast/page
     */
    public function getWebcastPage($pageNo = 1, $sec = '', $status = '', $expired = '', $createdTime = '')
    {
        $url = $this->url . 'webcast/page';

        $data = [
            'loginName' => $this->loginName,
            'password' => $this->password,
            'pageNo' => $pageNo,
            'sec' => $sec,
            'status' => $status,
            'expired' => $expired,
            'createdTime' => $createdTime,
        ];

        $result = Utils::curlPost($url, $data);
        return json_decode($result, true);
    }

    /*
     * 分页获取正在进行的直播列表
     * 对应接口 5.21 webcast/ongoing/page
     */
    public function getWebcastOngoingPage($pageNo = 1, $pageSize = 20, $sec = '')
    {
        $url = $this->url . 'webcast/ongoing/page';

        $data = [
            'loginName' => $this->loginName,
            'password' => $this->password,
            'pageNo' => $pageNo,
            'pageSize' => $pageSize,
            'sec' => $sec,
        ];

        $result = Utils::curlPost($url, $data);
        return json_decode($result, true);
    }

    /*
     * 分页同步点播数据---根据时间段获取已发布点播列表
     * 对应接口 5.23 webcast/vod/sync
     */
    public function getWebcastVodSync($pageNo, $startTime, $endTime, $sec = '')
    {
        $url = $this->url . 'webcast/vod/sync';

        $data = [
            'loginName' => $this->loginName,
            'password' => $this->password,
            'pageNo' => $pageNo,
            'startTime' => $startTime,
            'endTime' => $endTime,
            'sec' => $sec,
        ];

        $result = Utils::curlPost($url, $data);
        return json_decode($result, true);
    }

    /*
     * 获取直播配置信息
     * 对应接口 5.8 webcast/setting/info
     */
    public function getWebcastSettingInfo($webcastId, $sec = '')
    {
        $url = $this->url . 'webcast/setting/info';

        $data = [
            'loginName' => $this->loginName,
            'password' => $this->password,
            'webcastId' => $webcastId,
            'sec' => $sec,
        ];

        $result = Utils::curlPost($url, $data);
        return json_decode($result, true);
    }


}